//
//  SEGDoorNoCooperationOrCertificationView.m
//  Commom
//
//  Created by Clavis on 2020/6/5.
//

#import "SEGDoorNoCooperationOrCertificationView.h"
#import "SEGKnowOpenDoorViewController.h"
#import "SEGBasicBussinessEnums.h"
#import "SEGProfileManager.h"
#import <ReactiveObjC/ReactiveObjC.h>

@interface SEGDoorNoCooperationOrCertificationView ()

@property (nonatomic, strong) UIView *containerView;

@end

@implementation SEGDoorNoCooperationOrCertificationView

+ (void)show {
    SEGDoorNoCooperationOrCertificationView *view = [[SEGDoorNoCooperationOrCertificationView alloc] init];
    [[UIApplication sharedApplication].keyWindow addSubview:view];
    view.frame = view.superview.bounds;
    CGFloat y = view.containerView.y;
    view.containerView.y = LYScreenHeight();
    view.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0];
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        view.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.5];
        view.containerView.y = y;
    } completion:nil];
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.containerView = [[UIView alloc] init];
    self.containerView.backgroundColor = UIColor.whiteColor;
    [self addSubview:self.containerView];
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(160);
    }];
    
    UILabel *tipLabel = ({
        UILabel *label = [[UILabel alloc] init];
        label.numberOfLines = 0;
        label.text = @"很遗憾，您所在的小区，尚未开通“一键开门”，暂时无法使用该功能。";
        label.textColor = kColor_1;
        label.font = kFont_16;
        label.numberOfLines = 0;
        [self.containerView addSubview:label];
        label;
    });
    [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(45);
        make.right.mas_equalTo(-45);
        make.top.mas_equalTo(45);
    }];
    
    SEGEnumOpendoorShowType showType = [SEGProfileManager openDoorType];
    if (showType == SEGEnumOpendoorShowType_NoCertification) {
        [self.containerView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.mas_equalTo(0);
            make.height.mas_equalTo(230);
        }];
        tipLabel.text = @"须经物业管理处认证成功的住户方可使用该功能";
        
        UIButton *applyButton = ({
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            [button setTitle:@"申请住户认证" forState:UIControlStateNormal];
            [button setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
            button.backgroundColor = kColor_T;
            button.titleLabel.font = kFont_15;
            [self.containerView addSubview:button];
            button;
        });
        [applyButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(tipLabel.mas_bottom).offset(38);
            make.width.mas_equalTo(180);
            make.height.mas_equalTo(40);
            make.centerX.mas_offset(0);
        }];
        [[applyButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            UIViewController *submitUserAtuhVC = nil;
            submitUserAtuhVC = [[SEGUhomeMediator sharedInstance] seg_initSubmitUserAtuhViewController:nil];
            submitUserAtuhVC.hidesBottomBarWhenPushed = YES;
            UIViewController *currentVC = [[SEGUhomeMediator sharedInstance] seg_getCurrentController:nil];
            [currentVC.navigationController pushViewController:submitUserAtuhVC animated:YES];
            [self removeFromSuperview];
        }];
    }
    
    // 关闭按钮
    UIButton *closeButton = ({
       UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
       [button setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_opendoor_close")] forState:UIControlStateNormal];
       [self.containerView addSubview:button];
       button;
    });
    [closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
       make.width.height.mas_equalTo(40);
       make.bottom.mas_equalTo(-10);
       make.centerX.mas_offset(0);
    }];
    [[closeButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            self.containerView.y = LYScreenHeight();
            self.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0];
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }];

    // 帮助按钮
    UIButton *helpButton = ({
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_opendoor_problem")] forState:UIControlStateNormal];
        [self.containerView addSubview:button];
        button;
    });
    [helpButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.width.height.mas_equalTo(40);
        make.centerY.equalTo(closeButton.mas_centerY);
    }];
    [[helpButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [self removeFromSuperview];
        UIViewController *currentVC = [[SEGUhomeMediator sharedInstance] seg_getCurrentController:nil];
        SEGKnowOpenDoorViewController *knowOpenDoor = [SEGKnowOpenDoorViewController new];
        knowOpenDoor.hidesBottomBarWhenPushed = YES;
        [currentVC.navigationController pushViewController:knowOpenDoor animated:YES];
    }];
    
    [self addSwipeGesture];
}

#pragma mark - 手势
- (void)addSwipeGesture {
    UISwipeGestureRecognizer * recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
    [self addGestureRecognizer:recognizer];
}

- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer{
    if(recognizer.direction == UISwipeGestureRecognizerDirectionDown) {
        [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            self.containerView.y = LYScreenHeight();
            self.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0];
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }
}

@end
